<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8" />
    <title>02_复习-拷贝</title>
</head>

<body>
    <script>
        // 浅拷贝
        // var person = {
        //     name:'zs',
        //     age:18,
        //     foods:['水饺','麻辣烫','盖儿饭']
        // }


        // function lightCopy(obj){
        //     var newObj = {};

        //     for(var x in obj){
        //         newObj[x] = obj[x];
        //     }

        //     // obj.name = 'ls';
        //     obj.foods[1] = 666;
        //     console.log(obj);
        //     console.log(newObj);

        // }

        // lightCopy(person)


        // 深拷贝
        var obj1 = {
            a1: 'zs',
            a2: 18,
            a3: true,
            a4: undefined,
            a5: null,
            a6: [1, 2, 3],
            a7: function () {
                console.log(111);
            },
            a8: { name: 'ls' },
            a9: /\w{6,8}/
        }

        function deepCopy2(target) {
            var result = {}
            for (var key in target) {
                result[key] = typeof target[key] === 'object' ? deepCopy2(target[key]) : target[key]
            }
            return result
        }
        var obj3 = deepCopy2(obj1)
        console.log(obj1)
        console.log(obj3)



    </script>
</body>

</html>